var BarcodeInputMethod = IBImgInputMethod.extend({
	size: 2,
	code: 'EAN',
	menu_node: null,
	size_map: null,
	code_map: null,
	instruction_map: null,
	constructor: function()
	{
		var x = this;
		var size_handler = function(pos) { x.size = x.size_map[pos]; return true; }
		var code_handler = function(pos) { x.code = x.code_map[pos]; return true; }
		var size_node;
		var code_node;
		var i;

		// Size node...
		size_node = new GFMenuNode($GFS('Set output size'));
		size_node.SetItem(0,$GFS('Small'),size_handler);
		size_node.SetItem(1,$GFS('Medium'),size_handler);
		size_node.SetItem(2,$GFS('Large'),size_handler);
		size_node.SetItem(3,$GFS('Extra Large'),size_handler);
		this.size_map = new Array(1,2,3,5);

		// Code node...
		this.code_map = new Array('EAN','UPC','ISBN','39','128','128B','128C','I25','128RAW','CBR','MSI','PLS','93');
		this.instruction_map = {
			'EAN': '12 or 13 numbers',  // EAN
			'UPC': '11 or 12 numbers',  // UPC
			'ISBN': '9 or 10 numbers',  // ISBN
			'39': '[0-9A-Z-. *$/+%] only',  // 39
			'128': 'ASCII code only',  // 128
			'128B': 'ASCII code only',  // 128B
			'128C': 'ASCII code only',  // 128C
			'I25': null, // I25
			'128RAW': null, // 128RAW
			'CBR': null, // CBR
			'MSI': 'Numbers only', // MSI
			'PLS': null, // PLS
			'93': 'ASCII code only' // 93
		};
			
		code_node = new GFMenuNode($GFS('Set encoding'));
		for(i=0;i<this.code_map.length;i++) {
			code_node.SetItem(i,this.code_map[i],code_handler);
		}
		this.menu_node = new GFMenuNode('Barcode'+$GFS('Input Method Settings'));
		this.menu_node.SetItem(0,$GFS('Set output size'),size_node);
		this.menu_node.SetItem(1,$GFS('Set encoding'),code_node);
	},
	IMName: function()
	{
		return 'Barcode';
	},
	GetLanguage: function()
	{
		return 'util';
	},
	GetMenuNode: function()
	{
		return this.menu_node;
	},
	onInternalBufferStart: function(iwin)
	{
		// try to see if there is an instruction
		iwin.SetCompleteKeyCode(13,0);
		var inst = this.instruction_map[this.code];

		if(inst) inst = this.code + ': ' + inst;
		else inst = this.code;

		iwin.SetInstruction($GFS('Press Enter to complete') + ' (' + inst +')');
		return false;
	},
	SuggestionType: function()
	{
		return null;
	},
	isKeyValid: function(key,m)
	{
		if(m != 0) return false;

		if((97 <= key && key <= 122) || (65 <= key && key <= 90) || (48 <= key && key <= 58)) return true;
		else return false;
	},
	onActive: function()
	{
		this.ime.SetPreeditText($GFS('You can press Esc Esc 6 to configure this input method'));
	},
	GetHTMLPostfix: function()
	{
		return '<br />&nbsp;';
	},
	GetImageUrl: function(buf)
	{
		return 'http://www.ajaxime.com/~wonghang/php-barcode/barcode.php?scale='+this.size+'&mode=png&encoding='+this.code+'&code=' + encodeURI(buf);
	},
	SelectHandler: function(obj,pos)
	{
		this.size = this.list_size[pos];
		this.ime.StartInternalBuffer(obj);
	}
});
